package com.huiminpay.merchant.controller;

import com.huiminpay.merchant.common.util.SecurityUtil;
import com.huiminpay.transaction.api.dto.PayChannelDTO;
import com.huiminpay.transaction.api.dto.PayChannelParamDTO;
import com.huiminpay.transaction.api.dto.PlatformChannelDTO;
import com.huiminpay.transaction.api.service.PlatformChannelServiceApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商户平台‐服务类型和支付渠道相关", description = "商户平台‐服务类型和支付渠道相关")
@Slf4j
@RestController
public class PlatformChannelController {

    @Reference
    private PlatformChannelServiceApi platformChannelService;


    @ApiOperation("获取平台服务类型")
    @GetMapping("my/platform-channels")
    public List<PlatformChannelDTO> findAllPlatformChannel() {
        return platformChannelService.findAllPlatformChannel();
    }

    @ApiOperation("为应用绑定服务类型")
    @PostMapping(value = "/my/apps/{appId}/platform‐channels")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "应用id", name = "appId", dataType = "string", paramType =
                    "path"),
            @ApiImplicitParam(value = "服务类型code", name = "platformChannelCodes", dataType = "string", paramType = "query")
    })
    public void boundPlatformChannelForApp(@PathVariable("appId") String appId,
                                           @RequestParam("platformChannelCodes") String platformChannelCodes) {
        platformChannelService.bindPlatformChannelForApp(appId, platformChannelCodes);
    }

    @ApiOperation("查询应用是否绑定某个服务类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用appId", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "platformChannel", value = "服务类型", required = true, dataType = "String", paramType = "query")})
    @GetMapping("/my/merchants/apps/platformchannels")
    public String findAppBindPlatformChannel(@RequestParam("appId") String appId,
                                             @RequestParam("platformChannel") String platformChannel) {
        return platformChannelService.existBindPlatformForApp(appId, platformChannel);
    }


    //==========================================


    @ApiOperation("根据平台服务类型获取支付渠道列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "platformChannelCode", value = "服务类型编码", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping(value = "/my/pay‐channels/platform‐channel/{platformChannelCode}")
    public List<PayChannelDTO> queryPayChannelByPlatformChannel(@PathVariable String platformChannelCode) {
        return platformChannelService.queryPayChannelByPlatformChannel(platformChannelCode);
    }

    @ApiOperation("商户配置支付渠道参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "payChannelParam", value = "商户配置支付渠道参数", required = true, dataType = "PayChannelParamDTO", paramType = "body")
    })
    @PostMapping("/my/pay-channel-params")
    public PayChannelParamDTO queryPayChannelParam(@RequestBody PayChannelParamDTO payChannelParam) {
        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParam.setMerchantId(merchantId);
        PayChannelParamDTO payChannelParamDTO = platformChannelService.savePayChannelParam(payChannelParam);
        return payChannelParamDTO;
    }

    @ApiOperation("获取指定应用指定服务类型下的某个原始支付渠道参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用id", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "payChannel", value = "第三方渠道code", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "platformChannel", value = "服务类型", required = true,
                    dataType = "String", paramType = "path")})
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
    public PayChannelParamDTO queryPayChannelParam(@PathVariable String appId,
                                                   @PathVariable String payChannel,
                                                   @PathVariable String platformChannel) {
        PayChannelParamDTO payChannelParamDTO = platformChannelService.queryParamByAppPlatformAndPayChannel(appId, payChannel, platformChannel);
        return payChannelParamDTO;
    }

    @ApiOperation("获取指定应用指定服务类型下所包含的原始支付渠道参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用id", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "platformChannel", value = "服务类型", required = true,
                    dataType = "String", paramType = "path")})
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
    public List<PayChannelParamDTO> queryPayChannelParamList(@PathVariable String appId,
                                                             @PathVariable String platformChannel){
        List<PayChannelParamDTO> payChannelParamDTOList = platformChannelService.queryParamListByAppPlatformAndPayChannel(appId, platformChannel);
        return payChannelParamDTOList;
    }
}
